#include "dialog.h"
#include "ui_dialog.h"
#include <QTimer>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    Qt::WindowFlags flags=Qt::Dialog;
    flags |=Qt::WindowMinMaxButtonsHint;
    flags |=Qt::WindowCloseButtonHint;
    this->setWindowFlags(flags);
    this->setWindowTitle("商户配置机器台量需求分析");
    //
    chart = new QChart();
    chart->setTitle("横轴: 店面桌数(按4人/桌计), 纵轴: 排队等待时间(分钟)");
    axisX = new QValueAxis;
    axisX->setRange(12, 50); //设置范围
    axisX->setLabelFormat("%u"); //设置刻度的格式
    axisX->setGridLineVisible(true); //网格线可见
    axisX->setTickCount(50-12); //设置多少格
    axisX->setMinorTickCount(1); //设置每格小刻度线的数目
    //
    axisY = new QValueAxis;
    axisY->setRange(0, 60); //设置范围
    axisY->setLabelFormat("%u"); //设置刻度的格式
    axisY->setGridLineVisible(true); //网格线可见
    axisY->setTickCount(7); //设置多少格
    axisY->setMinorTickCount(10); //设置每格小刻度线的数目
    //
    chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    ui->horizontalLayout->addWidget(chartView);
    //
    ui->lineEdit_ftl->setPlaceholderText("翻台率");
    ui->lineEdit_dbl->setPlaceholderText("1-打包比例");
    //
    QLineSeries *seriesLine = new QLineSeries();
    seriesLine->setName("分割线");
    seriesLine->setColor(QColor(255, 255, 0));
    for(int i=12; i<=50; i++){
        seriesLine->append(i, 10);
    }
    chart->addSeries(seriesLine);
    chart->setAxisX(axisX, seriesLine);
    chart->setAxisY(axisY, seriesLine);
    //
    Init();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::Init()
{
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->verticalHeader()->setVisible(false);
    ui->tableWidget->horizontalHeader()->setVisible(true);
    ui->tableWidget->setShowGrid(true);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); //桌数
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); //人数
    ui->tableWidget->horizontalHeader()->setFixedHeight(30);
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    QStringList tableHeader;
    tableHeader << "桌数" << "m(人数)";
    ui->tableWidget->setHorizontalHeaderLabels(tableHeader);

    QTimer::singleShot(500, [=](){
        for(int i=12; i<=50; i++){
            int nCurRow = ui->tableWidget->rowCount();
            ui->tableWidget->setRowCount(nCurRow+1);
            ui->tableWidget->setRowHeight(nCurRow, 21);
            QTableWidgetItem *item = new QTableWidgetItem(QString::number(i));
            item->setTextAlignment(Qt::AlignCenter);
            ui->tableWidget->setItem(nCurRow, 0, item);
            item = new QTableWidgetItem(QString::number((i)*4));
            item->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            ui->tableWidget->setItem(nCurRow, 1, item);
        }
    });
}

void Dialog::on_pushButton_clicked()
{
    double rgsd = ui->lineEdit_rgsd->text().toDouble();
    double rgsl = ui->lineEdit_rgsl->text().toDouble();
    double jqsd = ui->lineEdit_jqsd->text().toDouble();
    double jqrsl = ui->lineEdit_jqrsl->text().toDouble();
    double rgxzl = ui->lineEdit_rgxzl->text().toDouble();
    double miu = rgsd*(rgsl - jqrsl * rgxzl) + jqsd*jqrsl;

    double ftl = ui->lineEdit_ftl->text().toDouble(); //翻台率3
    double dbl = ui->lineEdit_dbl->text().toDouble(); //除了打包率
    int gfq = ui->lineEdit_gfq->text().toDouble(); //高峰期时间

    int RobotCount = jqrsl;

    int nRowCount = ui->tableWidget->rowCount();
    QLineSeries *series = new QLineSeries();
    series->setName(QString("%1台").arg(RobotCount));
    QString Log;
    for(int i=0; i<nRowCount; i++){
        int zhuoshu = ui->tableWidget->item(i, 0)->text().toInt();
        int m = zhuoshu*4;

        double lamada = m*ftl/dbl/gfq;

        double rou = lamada/miu;
        double P0 = (1 - rou) / (1 - pow(rou, m+1));
        double Pm = pow(rou, m) * P0;
        Pm = QString::number(Pm, 'f', 2).toDouble();
        double lamada_shiji = (1 - Pm)*lamada;
        double duichangL = rou/(1 - rou) - (m + 1) * pow(rou, m+1) / (1 - pow(rou, m+1));
        double pduichangL = fabs(duichangL - (1 - P0));
        double dengdaijiashouyinshijian = duichangL / lamada_shiji;
        double dengdaishijian = pduichangL / lamada_shiji;
        QString Wq = QString::number(dengdaishijian, 'f', 2);
        qDebug() << "桌数=" << zhuoshu << "台数=" << RobotCount
                 << lamada << miu << rou << P0 << Pm << lamada_shiji << duichangL
                 << pduichangL << dengdaijiashouyinshijian << dengdaishijian
                 << Wq;
        QString szLog = QString("%1,%2").arg(zhuoshu).arg(Wq);
        Log.append(szLog).append("\n");

        series->append(zhuoshu, Wq.toDouble());
    }
    chart->addSeries(series);
    chart->setAxisX(axisX, series);
    chart->setAxisY(axisY, series);
//    chart->createDefaultAxes();
    ui->textEdit->setText(Log);
}

void Dialog::on_pushButton_2_clicked()
{
    QList<QAbstractSeries *> list = chart->series();
    while(!list.isEmpty()){
        QAbstractSeries *series = list.takeAt(0);
        qDebug() << series->name();
        delete series; series = nullptr;
    }
    ui->textEdit->clear();

    QLineSeries *seriesLine = new QLineSeries();
    seriesLine->setName("分割线");
    seriesLine->setColor(QColor(255, 255, 0));
    for(int i=12; i<=50; i++){
        seriesLine->append(i, 10);
    }
    chart->addSeries(seriesLine);
    chart->setAxisX(axisX, seriesLine);
    chart->setAxisY(axisY, seriesLine);
}

void Dialog::on_pushButton_3_clicked()
{
    QScreen *screen = QGuiApplication::primaryScreen();
    QPixmap p = screen->grabWindow(chartView->winId());
    QImage image = p.toImage();

    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                      "/home",
                                                      QFileDialog::ShowDirsOnly
                                                      | QFileDialog::DontResolveSymlinks);
    image.save(QString("%1/chart.png").arg(dir));
}
